package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.entity.BinInfoEntity;
import com.glory.dgcpcpda.entity.EdcSetBinEntity;

import java.text.NumberFormat;
import java.util.List;

/**
 * FileName: BinInfoListAdapter
 * Author: XJH
 * Date: 2020/5/29 15:25
 * Description:
 * History:
 */
public class MesBinInfoAdapter extends BaseQuickAdapter<EdcSetBinEntity, BaseViewHolder> {
    private int mMainQTY = -1;

    public MesBinInfoAdapter(@Nullable List<EdcSetBinEntity> data) {
        super(R.layout.item_bin_info_list, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, EdcSetBinEntity item) {
        helper.setIsRecyclable(false);
        helper.setText(R.id.list_tv1, item.getNAME());
        helper.setText(R.id.list_tv2, item.getDESCRIPTION());
        helper.setText(R.id.etBinQTY, item.getVALUE());
        helper.setGone(R.id.list_tv4, true);

        helper.setGone(R.id.list_tv3, false);
        helper.setGone(R.id.etBinQTY, true);
        try {
            calculation(helper.getView(R.id.list_tv4), Integer.parseInt(item.getVALUE()));
        } catch (NumberFormatException e) {
            calculation(helper.getView(R.id.list_tv4), 0);
            e.printStackTrace();
        }

        EditText etBinQTY = helper.getView(R.id.etBinQTY);
        etBinQTY.setText(String.valueOf(item.getVALUE()));
        etBinQTY.setEnabled(item.getNAME().equals("BIN9") || item.getNAME().equals("BIN10") || item.getNAME().equals("BIN12") || item.getNAME().equals("BIN2"));
        setEditListener(etBinQTY, item, helper.getView(R.id.list_tv4));
    }

    private void calculation(TextView view, double binQTY) {
        if (mMainQTY == -1) {
            return;
        }
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(3);
        String result = numberFormat.format(((float) (binQTY) / (float) mMainQTY) * 100);
        view.setText(result + "%");
    }

    public void setMainQTY(int mainQTY) {
        this.mMainQTY = mainQTY;
    }

    private void setEditListener(EditText editText, EdcSetBinEntity item, TextView view) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    item.setVALUE(s.toString());
                    calculation(view, Integer.parseInt(s.toString()));
                } catch (NumberFormatException e) {
                    calculation(view, 0);
                    e.printStackTrace();
                }

            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
